package com.android.dongshouzhi.fragment.detail;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.dongshouzhi.R;
import com.android.dongshouzhi.activity.DirectBuyGoodsDetailActivity;
import com.android.dongshouzhi.adapt.GridDirectBuyGoodsDetaillikeAdapt;
import com.android.dongshouzhi.fragment.BaseLazyFragment;
import com.android.dongshouzhi.model.detail.DirectBuyGoodsDetail;
import com.android.dongshouzhi.utils.ConstantsUrl;
import com.android.dongshouzhi.utils.ReleaseManager;
import com.android.dongshouzhi.view.MyGridView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * 商品图文详情
 */
public class Fragment_DirectBuy_GoodsDetail_DetailWeb extends BaseLazyFragment {

    @BindView(R.id.webView)
    WebView webView;
    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.web_nodata)
    TextView nodata;
    @BindView(R.id.ll_bottom_like_desc)
    LinearLayout llLike;
    @BindView(R.id.detail_like_grid)
    MyGridView detailTJGrid;
    private FragmentActivity mcontext;
    private View view;
    private static final String ARG_PARAM1 = "param1";
    private String mParam1;
    private GridDirectBuyGoodsDetaillikeAdapt adapter_TJ;
    private List<DirectBuyGoodsDetail.DataBean.TjListBean> listdata;

    private Unbinder unbinder;
    public Fragment_DirectBuy_GoodsDetail_DetailWeb() {


    }

    public static Fragment_DirectBuy_GoodsDetail_DetailWeb newInstance(String param1) {
        Fragment_DirectBuy_GoodsDetail_DetailWeb fragment = new Fragment_DirectBuy_GoodsDetail_DetailWeb();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    protected void initPrepare() {

    }

    @Override
    protected void onInvisible() {

    }


    @Override
    protected void initData() {
        initUI();
    }


    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_directbuy_goods_detail_web, container, false);
        mcontext = getActivity();
        unbinder = ButterKnife.bind(this, view);
        return view;
    }


    private void initUI() {

        if (mParam1 == null || mParam1.equals("")) {
            progress.setVisibility(View.GONE);
            nodata.setVisibility(View.VISIBLE);

        } else {

            webView.setWebViewClient(new MyWebViewClient());
            webView.setWebChromeClient(new WebChromeClient() {
                @Override
                public void onProgressChanged(WebView view, int newProgress) {
                    super.onProgressChanged(view, newProgress);
                    if (newProgress == 100) {
                        //报空的异常
                        if(progress!=null)
                        progress.setVisibility(View.GONE);
                    }
                }
            });
            nodata.setVisibility(View.GONE);
            //宽度适应屏幕
            String js = "<style>\n" +
                    "img{\n" +
                    " max-width:100%;\n" +
                    "height:auto\n" +
                    "}\n" +
                    "</style>" + mParam1;
            ReleaseManager.printLog("js ",js);
            webView.loadDataWithBaseURL(null, js, "text/html", "utf-8", null);
        }

        detailTJGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (listdata != null) {
                    ToDetailActivity(listdata.get(position).getId());
                }
            }
        });
    }

    public void ToDetailActivity(String id) {
        Intent intent = new Intent(mcontext, DirectBuyGoodsDetailActivity.class);
        intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TO_DETAIL_ID, id);
        startActivity(intent);
    }

    public void setTJ_List(List<DirectBuyGoodsDetail.DataBean.TjListBean> listdata) {
        this.listdata = listdata;
        if (listdata != null) {
            adapter_TJ = new GridDirectBuyGoodsDetaillikeAdapt(mcontext, listdata);
            detailTJGrid.setAdapter(adapter_TJ);
            llLike.setVisibility(View.VISIBLE);
        } else {
            llLike.setVisibility(View.GONE);
        }
    }

    class MyWebViewClient extends WebViewClient {
        //拦截网址
    }

    @Override
    public void onDestroyView() {
        if(webView!=null) {
            webView.destroy();
        }
        webView = null;
        unbinder.unbind();
        super.onDestroyView();
    }
}
